package com.abacus.lcy.m;

public class Attribute implements Value {

	private Long id;

	private String key;

	private String values;

	private String format;

	private String valueParser;

	private Integer valueParserCode;

	private Object value;

	private boolean valuesChanged = false;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValues() {
		return values;
	}

	public void setValues(String values) {
		if (null != values) {
			if (!values.equals(this.values)) {
				this.values = values;
				valuesChanged = true;
			} else {
				valuesChanged = false;
			}
		} else {
			if (null != this.values) {
				this.values = values;
				valuesChanged = true;
			} else {
				valuesChanged = false;
			}
		}
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getValueParser() {
		return valueParser;
	}

	public void setValueParser(String valueParser) {
		this.valueParser = valueParser;
	}

	public Integer getValueParserCode() {
		return valueParserCode;
	}

	public void setValueParserCode(Integer valueParserCode) {
		this.valueParserCode = valueParserCode;
	}

	public Object getValue() {
		if (valuesChanged) {
			try {
				Class clazz = Class.forName(valueParser);
				ValueParser parser = (ValueParser) clazz.getMethod("getInstance", null).invoke(null, null);
				value = ((ValueParser) parser).valueOf(this);
			} catch (Exception e) {
				throw new IllegalArgumentException(e);
			}
		}
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public String toString() {
		return "Attribute [id=" + this.getId() + ", key=" + this.getKey() + ", value=" + this.getValue() + "]";
	}

}
